package com.tianzj.mvc.system.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tianzj.mvc.system.service.SysUserRoleService;
import com.tianzj.util.AjaxJson;



@RestController
@RequestMapping("sysUserRole")
public class SysUserRoleController {
	
	private Logger logger  =  LoggerFactory.getLogger(SysUserRoleController.class );

	@Autowired
	private SysUserRoleService sysUserRoleService;
	/**
	 * 根据用户查询角色
	 * @param userId
	 * @return
	 */
	@PostMapping("getRoleIdByUserId")
	public AjaxJson getRoleIdByUserId(Long userId){
		AjaxJson ajaxJson = new AjaxJson();
		List<Long> roleIds = sysUserRoleService.getRoleIdByUserId(userId);
		ajaxJson.setObj(roleIds);
		return ajaxJson;
	}
	
	@PostMapping("addUserRole")
	public ModelAndView addUserRole(Long userId,String roleIds){
		try {
			sysUserRoleService.deleteByUserId(userId);
			sysUserRoleService.addUserRole(userId,roleIds);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return new ModelAndView("redirect:/sysUser/userList");
	}
	
}
